function retval = isrealvarname(testname)
%ISREALVARNAME True for valid variable name.
%   ISREALVARNAME(S) is true if S is a valid MATLAB variable name.
%   A valid variable name is a character string of letters, digits and
%   underscores, with length <= namelengthmax and the first character a letter.
%
%   See also ISKEYWORD, ISVARNAME, NAMELENGTHMAX.
%
%   Revised, since the Windows version returns true for INVALID names!

% Version:  v0.8a
% Build:    9102300
% Date:     Oct-23 2009, 12:41 AM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1
    error( ...
        'BVQXtools:TooFewArguments',...
        'Too few arguments.'...
    );
end

% test only valid char inputs
if ischar(testname) && ...
   ~isempty(testname) && ...
    numel(testname) == size(testname, 2) && ...
    all(double(testname) < 123 & double(testname) > 47) && ...
    testname(1) > 64 && ...
   (testname(1) < 91 || ...
    testname(1) > 96) && ...
   ~any(any(double(testname(ones(12, 1), :)) == (([58:64, 91:94, 96])' * ones(1, numel(testname)))))
    retval = true;
else
    retval = false;
end
